package qaConsultant;

import java.io.File;

import java.io.FileInputStream;

import java.io.IOException;
import java.util.concurrent.TimeUnit;


import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
import jxl.write.Label;

import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;

import org.junit.*;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;




public class CNST_Positive {
	WebDriver qq;
	String qlink, hptext;
	String FName, LName, Email, Phone, Country;
			
	@Before
		
	public void setUp() throws Exception {
		qq = new FirefoxDriver();
	    qlink = "http://qaonair.com/";
	    qq.get(qlink);
	    qq.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
	  }

	
    @Test
    
    public void testNewConsultant() throws BiffException, IOException, RowsExceededException, WriteException {
    	
    	String qlink1 = qlink + "work-qa-onair/";
    	String xlFilePath="C:\\Users\\Sushma\\Desktop\\Selenium LIVE project - Jan 2014\\My Code\\DDF97WB2.xls";
 		String fname,lname,email,phone,country,Successmsg="NULL", act_msg="NULL", Result="NULL";
    	FileInputStream fi = new FileInputStream(xlFilePath);
    	Workbook w = Workbook.getWorkbook(fi);
    	Sheet s = w.getSheet(0);
    	int totalrows=s.getRows();  
    	
    	for(int row=1; row < totalrows;row++)
    	{
    	  
    		fname = s.getCell(0,row).getContents();
    		lname = s.getCell(1,row).getContents();
    		email = s.getCell(2,row).getContents();
    		phone= s.getCell(3,row).getContents();
    		country= s.getCell(4,row).getContents();
    		Successmsg= s.getCell(5,row).getContents();
    		//input values to consultant fields
    		qq.navigate().to(qlink1);
    		
    		
    		qq.findElement(By.id("inf_field_FirstName")).sendKeys(fname);
    		qq.findElement(By.id("inf_field_LastName")).sendKeys(lname);
    		qq.findElement(By.id("inf_field_Email")).sendKeys(email);
    		qq.findElement(By.id("inf_field_Phone1")).sendKeys(phone);
    		new Select(qq.findElement(By.id("inf_field_Country"))).selectByValue(country);
    	
    		qq.findElement(By.cssSelector("input[type = \"submit\"]")).click();
    		qq.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
    		act_msg = qq.findElement(By.xpath("/html/body/div/div/div/main/article/header/h1")).getText();
    		
    		
    		if (Successmsg.equals(act_msg))
    		{
    			Result = "Pass";
    		}
    		else {
    			Result = "Fail";
    		}
    		System.out.println(row+". "+Result);
    		
    	}
    	
    	WritableWorkbook wb = Workbook.createWorkbook(new File("C:\\Users\\Sushma\\Desktop\\Selenium LIVE project - Jan 2014\\My Code\\Opostve.xls"));
    	//getWorkbook(File())
    	WritableSheet sheet = wb.createSheet("This",0);
    	
    	//getSheet("Sheet1")
    	
    	Label I = new Label(0,0,"RESULT");
    	sheet.addCell(I);
    	
    	Label I1 = new Label(1,0,"SCSS_MSG");
    	sheet.addCell(I1);
    	
    	Label J = new Label (0,1,Result);
    	sheet.addCell(J);
    	
    	Label J1 = new Label (1,1,Successmsg);
    	sheet.addCell(J1);
    	 	
       	wb.write();
    	wb.close();
      
    }

	  @After
	  public void tearDown() throws Exception {
	    qq.quit();
	  }  
}

